package com.example.invoke.extend.bootstrap;

import com.example.invoke.extend.listener.BaseListener;
import com.example.invoke.xsd.SpringContentUtil;
import com.example.invoke.xsd.xml.CustomConfig;
import com.example.invoke.xsd.xml.ListenerConfig;
import org.springframework.util.StringUtils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class ServerBoot implements BootStrap {
    private CustomConfig customConfig;

    public ServerBoot() {}

    public ServerBoot(CustomConfig customConfig) {
        this.customConfig = customConfig;
    }

    @Override
    public void start() {
        bindListener();
    }

    private void bindListener() {
        List<ListenerConfig> listenerConfigList = customConfig.getListenerConfigList();
        System.out.println("ServerBoot start");
        System.out.println(listenerConfigList);
        for (ListenerConfig config : listenerConfigList) {
            String listener = config.getListener();
            if (StringUtils.isEmpty(listener)) continue;

            try {
                URL url = getUrl(config);
                BaseListener targetListener = (BaseListener) SpringContentUtil.getApplicationContext().getBean(Class.forName(config.getListener()));

                // socket绑定?
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
    }

    private URL getUrl(ListenerConfig config) throws MalformedURLException {
        return new URL("http", config.getIp(), Integer.valueOf(config.getIp()), null);
    }
}
